package cn.green.core.executor;

import cn.green.core.components.database.DBRouter;
import cn.green.core.components.database.entity.Database;
import cn.green.core.components.ddao.QDdl;
import cn.green.core.components.entity.DatabaseSql;
import cn.green.core.model.QField;
import cn.green.core.model.QTable;

/**
 * @author wang.yishu
 * @version 1.0
 * @className DdlExecutor
 * @description ddl执行器
 * @date 2024/11/22 15:37
 */
public class DdlExecutor {
    public DdlExecutor(QDdl ddl, DBRouter dbRouter) {
        this.dbRouter = dbRouter;
        this.ddl = ddl;
    }

    private final QDdl ddl;
    private final DBRouter dbRouter;

    /**
     * 执行建表操作
     * @param qTable    表结构
     */
    public void executeCrtTable(QTable qTable) {
        Database database = this.dbRouter.get(qTable.getSourceName());
        String crtTableSql = database.getDdlDialect().crtTableSql(qTable);
        this.ddl.crtTable(new DatabaseSql(database, crtTableSql, null));
    }

    /**
     * 执行建字段操作
     * @param qField    字段结构
     */
    public void executeCrtField(QField qField) {
        Database database = this.dbRouter.get(qField.getSourceName());
        String addFieldSql = database.getDdlDialect().addFieldSql(qField);
        this.ddl.addField(new DatabaseSql(database, addFieldSql, null));
    }

    /**
     * 执行修改字段操作
     * @param field    字段结构
     */
    public void executeUpdField(QField field) {
        Database database = this.dbRouter.get(field.getSourceName());
        String updFieldSql = database.getDdlDialect().updFieldSql(field);
        this.ddl.updField(new DatabaseSql(database, updFieldSql, null));
    }

    /**
     * 执行删除字段操作
     * @param field    字段结构
     */
    public void executeDelField(QField field) {
        Database database = this.dbRouter.get(field.getSourceName());
        String updFieldSql = database.getDdlDialect().delFieldSql(field);
        this.ddl.delField(new DatabaseSql(database, updFieldSql, null));
    }

}
